package com.offcn.scwproject.controller;

import com.offcn.dycommon.response.AppResponse;
import com.offcn.scwproject.pojo.*;
import com.offcn.scwproject.service.ProjectInfoSerivce;
import com.offcn.scwproject.vo.resp.ProjectDetailVo;
import com.offcn.scwproject.vo.resp.ProjectVo;
import com.offcn.utils.OssTemplate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.bouncycastle.util.Strings;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/project")
@Api(tags = "项目模块--包含图片上传,查询所有项目,项目的详情,所有标签,所有类型")
@Slf4j
public class ProjectInfoController {
    @Autowired
    private OssTemplate ossTemplate;
    @Autowired
    private ProjectInfoSerivce projectInfoSerivce;

    @ApiOperation("文件上传")
    //文件上传
    @PostMapping("/upload")
    public AppResponse<Map<String,Object>> upload(@RequestParam("files") MultipartFile[] files) throws IOException {

        Map map = new HashMap<String ,Object>();
        ArrayList<Object> list = new ArrayList<>();
        if(files != null && files.length > 0){
            for (MultipartFile file : files) {
                if(!file.isEmpty()){
                    String upload = ossTemplate.upload(file.getInputStream(), file.getOriginalFilename());
                    list.add(upload);
                }
            }
        }
        map.put("urls",list);
        log.debug("urls的地址是：{}===={}集合是：",list,ossTemplate);
        return AppResponse.ok(map);
    }

    //获取回报
    @ApiOperation("获取项目的回报列表")
    @GetMapping("/details/returns/{projectId}")
    public AppResponse<List<TReturn>> getReturnList(@PathVariable("projectId") Integer projectId){
        List<TReturn> returnList = projectInfoSerivce.getReturnList(projectId);
        return AppResponse.ok(returnList);
    }
    //查询所有的项目
    @ApiOperation("查询所有项目")
    @GetMapping("/all")
    public AppResponse<List<ProjectVo>> findAllProject(){
        //放项目的集合
        List<ProjectVo> list = new ArrayList<>();
        //查询所有项目
        List<TProject> porjectList = projectInfoSerivce.findPorjectList();
        //遍历
        for (TProject tProject : porjectList) {
            ProjectVo projectVo = new ProjectVo();
            BeanUtils.copyProperties(tProject,projectVo);
            //获取项目的图片
            List<TProjectImages> projectImageList = projectInfoSerivce.findProjectImageList(tProject.getId());
            //遍历项目的图片
            for (TProjectImages tProjectImages : projectImageList) {
                if (tProjectImages.getImgtype() == 0){
                    //设置头图
                    projectVo.setHeaderImage(tProjectImages.getImgurl());
                }
            }
            list.add(projectVo);
        }
        return AppResponse.ok(list);


    }
    //获取项目的详情
    @ApiOperation("获取项目信息详情")
    @GetMapping("/findProjectInfo/{projectId}")
    public AppResponse<ProjectDetailVo> findProjectInfo(@PathVariable("projectId") Integer projectId){
        //查询项目
        TProject projectByProject = projectInfoSerivce.findProjectByProjectId(projectId);
        ProjectDetailVo projectDetailVo = new ProjectDetailVo();
        //拷贝信息到详情的vo
        BeanUtils.copyProperties(projectByProject,projectDetailVo);
        //设置详细图片
        List<TProjectImages> projectImageList = projectInfoSerivce.findProjectImageList(projectId);
        ArrayList<String> Strings = new ArrayList<>();//放详图
        for (TProjectImages tProjectImages : projectImageList) {
            if(tProjectImages.getImgtype()==0){
                projectDetailVo.setHeaderImage(tProjectImages.getImgurl());
            }else {
                Strings.add(tProjectImages.getImgurl());
            }
        }
        projectDetailVo.setDetailsImage(Strings);
        //设置回报
        List<TReturn> returnList = projectInfoSerivce.getReturnList(projectId);
        projectDetailVo.setProjectReturns(returnList);
        return AppResponse.ok(projectDetailVo);
    }
    //所有的标签
    public AppResponse<List <TTag>> findAllTag(){
        return AppResponse.ok(projectInfoSerivce.findProjectTagList());
    }
    //所有的类型
    @ApiOperation("获取系统所有的项目分类")
    @GetMapping("/findAllType")
    public AppResponse<List<TType>> findAllType(){
        return AppResponse.ok(projectInfoSerivce.findProjectTypeList());
    }
    //获取回报详情
    @ApiOperation("获取回报信息")
    @GetMapping("/returns/info/{returnId}")
    public AppResponse<TReturn> findReturn(@PathVariable("returnId") Integer returnId){
        return AppResponse.ok(projectInfoSerivce.findProjectReturn(returnId));

    }


}
